package com.finance.controller;

import com.finance.service.FinanceDataSyncService;
import com.finance.util.DataType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/finance/sync")
public class FinanceDataSyncController {

    @Autowired
    private FinanceDataSyncService financeDataSyncService;

    /**
     * 手动触发指定日期范围的数据同步
     *
     * @param startDate 开始日期，格式：yyyy-MM-dd
     * @param endDate 结束日期，格式：yyyy-MM-dd
     * @return 同步结果
     */
    @GetMapping("/date")
    public ResponseEntity<Map<String, Object>> syncDataByDate(
            @RequestParam String startDate,
            @RequestParam String endDate,
            @RequestParam boolean forceUpdate) {
        log.info("手动触发数据同步，日期范围：{} 至 {}", startDate, endDate);
        Map<String, Object> result = new HashMap<>();
        try {
            LocalDate start = LocalDate.parse(startDate, DateTimeFormatter.ISO_DATE);
            LocalDate end = LocalDate.parse(endDate, DateTimeFormatter.ISO_DATE);
            
            for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
                financeDataSyncService.syncDataByDate(date.format(DateTimeFormatter.ISO_DATE), forceUpdate);
            }
            
            result.put("success", true);
            result.put("message", "数据同步任务已启动");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("手动触发数据同步失败", e);
            result.put("success", false);
            result.put("message", "数据同步失败：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 手动触发指定接口的数据同步
     *
     * @param dataType 数据类型代码
     * @param startDate 开始日期，格式：yyyy-MM-dd
     * @param endDate 结束日期，格式：yyyy-MM-dd
     * @return 同步结果
     */
    @GetMapping("/type")
    public ResponseEntity<Map<String, Object>> syncDataByType(
            @RequestParam String dataType,
            @RequestParam String startDate,
            @RequestParam String endDate,
            @RequestParam boolean forceUpdate) {
        log.info("手动触发数据同步，类型：{}，日期范围：{} 至 {}", dataType, startDate, endDate);
        Map<String, Object> result = new HashMap<>();
        try {
            DataType type = DataType.fromCode(dataType);
            if (type == null) {
                result.put("success", false);
                result.put("message", "无效的数据类型：" + dataType);
                return ResponseEntity.badRequest().body(result);
            }

            LocalDate start = LocalDate.parse(startDate, DateTimeFormatter.ISO_DATE);
            LocalDate end = LocalDate.parse(endDate, DateTimeFormatter.ISO_DATE);
            
            for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
                financeDataSyncService.syncDataByType(type, date, forceUpdate);
            }
            
            result.put("success", true);
            result.put("message", "数据同步任务已启动");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("手动触发数据同步失败", e);
            result.put("success", false);
            result.put("message", "数据同步失败：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 获取所有支持的数据类型
     *
     * @return 数据类型列表
     */
    @GetMapping("/types")
    public ResponseEntity<Map<String, Object>> getSupportedTypes() {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, String> types = new HashMap<>();
            for (DataType type : DataType.values()) {
                types.put(type.getCode(), type.getDescription());
            }
            result.put("success", true);
            result.put("data", types);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("获取数据类型列表失败", e);
            result.put("success", false);
            result.put("message", "获取数据类型列表失败：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }
} 